package com.ruoyi.hxf.controller;

import java.util.*;

import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hxf.domain.Subject;
import com.ruoyi.hxf.domain.Teacher;
import com.ruoyi.hxf.mapper.RosterMapper;
import com.ruoyi.hxf.mapper.TypeMapper;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hxf.domain.vo.SubjectVo;
import com.ruoyi.hxf.domain.bo.SubjectBo;
import com.ruoyi.hxf.service.ISubjectService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程的管理
 *
 * @author hxf
 * @date 2022-12-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/class/subject")
public class SubjectController extends BaseController {

    private final ISubjectService iSubjectService;
    private final ISysOssService iSysOssService;
    private final TypeMapper typeMapper;
    private final RosterMapper rosterMapper;

    /**
     * 查询课程的管理列表
     */
//    @SaCheckPermission("class:subject:list")
    @GetMapping("/list")
    public TableDataInfo<SubjectVo> list(SubjectBo bo, PageQuery pageQuery) {
        TableDataInfo<SubjectVo> subjectVoTableDataInfo = iSubjectService.queryPageList(bo, pageQuery);
        for (SubjectVo row : subjectVoTableDataInfo.getRows()) {
            if (row.getPicture() != null) {
                String[] split = row.getPicture().split(",");
                List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(Long.parseLong(split[0])));
                for (int i = 0; i < list.size(); i++) {
                    row.setUrl(list.get(i).getUrl());
                }
            }
        }
        return subjectVoTableDataInfo;
    }

    /**
     * 导出课程的管理列表
     */
//    @SaCheckPermission("class:subject:export")
    @Log(title = "课程的管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SubjectBo bo, HttpServletResponse response) {
        List<SubjectVo> list = iSubjectService.queryList(bo);
        ExcelUtil.exportExcel(list, "课程的管理", SubjectVo.class, response);
    }

    /**
     * 获取课程的管理详细信息
     *
     * @param subjectId 主键
     */
//    @SaCheckPermission("class:subject:query")
    @GetMapping("/{subjectId}")
    public R<SubjectVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long subjectId) {
        SubjectVo subjectVo = iSubjectService.queryById(subjectId);
        if(subjectVo.getPicture()!=null){
            String[] split = subjectVo.getPicture().split(",");
            StringBuffer stringBuffer = new StringBuffer();
            for (String s : split) {
                List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(Long.parseLong(s)));
                for (int i = 0; i < list.size(); i++) {
                    if (stringBuffer.length() == 0) {
                        stringBuffer.append(list.get(i).getUrl());
                    } else {
                        stringBuffer.append("," + list.get(i).getUrl());
                    }
                }
            }
            subjectVo.setUrl(stringBuffer.toString());
        }
        return R.ok(iSubjectService.queryById(subjectId));
    }

    /**
     * 新增课程的管理
     */
//    @SaCheckPermission("class:subject:add")
    @Log(title = "课程的管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SubjectBo bo) {
        if (bo.getPicture() != null) {
            String[] split = bo.getPicture().split(",");
            StringBuffer stringBuffer = new StringBuffer();
            for (String s : split) {
                List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(Long.parseLong(s)));
                for (int i = 0; i < list.size(); i++) {
                    if (stringBuffer.length() == 0) {
                        stringBuffer.append(list.get(i).getUrl());
                    } else {
                        stringBuffer.append("," + list.get(i).getUrl());
                    }
                }
            }
            bo.setUrl(stringBuffer.toString());
            iSubjectService.myUpdateSubject(bo);
        }
        return toAjax(iSubjectService.insertByBo(bo));
    }

    /**
     * 修改课程的管理
     */
//    @SaCheckPermission("class:subject:edit")
    @Log(title = "课程的管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SubjectBo bo) {
        if (bo.getPicture() != null) {
            String[] split = bo.getPicture().split(",");
            StringBuffer stringBuffer = new StringBuffer();
            for (String s : split) {
                List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(Long.parseLong(s)));
                for (int i = 0; i < list.size(); i++) {
                    if (stringBuffer.length() == 0) {
                        stringBuffer.append(list.get(i).getUrl());
                    } else {
                        stringBuffer.append("," + list.get(i).getUrl());
                    }
                }
            }
            bo.setUrl(stringBuffer.toString());
        }else {
            bo.setUrl(null);
        }
        bo.setUpdateBy(LoginHelper.getUsername());
        bo.setUpdateTime(DateUtils.getNowDate());
        return toAjax(iSubjectService.myUpdateSubject(bo));
    }

    /**
     * 删除课程的管理
     *
     * @param subjectIds 主键串
     */
//    @SaCheckPermission("class:subject:remove")
    @Log(title = "课程的管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{subjectIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] subjectIds) {
        return toAjax(iSubjectService.deleteWithValidByIds(Arrays.asList(subjectIds), true));
    }

    /**
     * 查询课程的管理列表
     */

    @GetMapping("/listTeachers/{classId}")
    public List<Teacher> listTeachers(@PathVariable("classId") Long classId) {
        List<Teacher> list = new ArrayList();
        String s = typeMapper.selectTeacherIdCount(classId);
        if (s != null && !s.equals("")) {
            String[] split = s.split(",");
            for (String s1 : split) {
                Teacher teacherMaster = rosterMapper.selectUserByIdRoleNameMaster(Long.valueOf(s1));
                if (teacherMaster != null) {
                    Teacher Exist = rosterMapper.selectTeacherMasterWhere(classId, Long.valueOf(s1));
                    if (Exist != null) {
                        list.add(teacherMaster);
                    } else {
                        Teacher Exist1 = rosterMapper.selectTeacherMasterWhereClass(Long.valueOf(s1));
                        teacherMaster.setTeacherName(teacherMaster.getTeacherName() + "(" + Exist1.getTypeName() + "班主任)");
                        list.add(teacherMaster);
                    }

                } else {
                    Teacher teacher = rosterMapper.selectUserByIdName(Long.valueOf(s1));
                    list.add(teacher);
                }
            }
        }
        return list;
    }
}
